/*     */ package com.zimbra.cs.extension;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.IOException;
/*     */ import java.net.URISyntaxException;
/*     */ import java.net.URL;
/*     */ import java.net.URLClassLoader;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.jar.Attributes;
/*     */ import java.util.jar.Attributes.Name;
/*     */ import java.util.jar.JarFile;
/*     */ import java.util.jar.Manifest;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ZimbraExtensionClassLoader
/*     */   extends URLClassLoader
/*     */ {
/*     */   public static final String ZIMBRA_EXTENSION_CLASS = "Zimbra-Extension-Class";
/*  40 */   private List<String> mExtensionClassNames = new ArrayList();
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public ZimbraExtensionClassLoader(URL[] urls, ClassLoader parent)
/*     */   {
/*  47 */     super(urls, parent);
/*  48 */     findExtensions();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   ZimbraExtensionClassLoader(URL url, String clazz)
/*     */   {
/*  59 */     super(new URL[] { url }, ZimbraExtensionClassLoader.class.getClassLoader());
/*     */     
/*  61 */     this.mExtensionClassNames.add(clazz);
/*     */   }
/*     */   
/*     */   private void findExtensions() {
/*  65 */     URL[] urls = getURLs();
/*  66 */     for (int i = 0; i < urls.length; i++) {
/*  67 */       File entry = null;
/*     */       try {
/*  69 */         entry = new File(urls[i].toURI());
/*     */       } catch (URISyntaxException e) {
/*  71 */         entry = new File(urls[i].getFile());
/*     */       }
/*  73 */       String className = getExtensionClassInManifest(entry);
/*  74 */       if (className != null) {
/*  75 */         this.mExtensionClassNames.add(className);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean hasExtensions() {
/*  81 */     return this.mExtensionClassNames.size() > 0;
/*     */   }
/*     */   
/*     */   public List<String> getExtensionClassNames() {
/*  85 */     return this.mExtensionClassNames;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private String getExtensionClassInManifest(File file)
/*     */   {
/*  95 */     Manifest man = null;
/*  96 */     if (file.isDirectory()) {
/*  97 */       File manifestFile = new File(file, "META-INF/MANIFEST.MF");
/*  98 */       if (manifestFile.exists())
/*     */       {
/*     */         try
/*     */         {
/* 102 */           man = new Manifest(new FileInputStream(manifestFile));
/*     */         } catch (IOException ioe) {
/* 104 */           if (ZimbraLog.extensions.isDebugEnabled()) {
/* 105 */             ZimbraLog.extensions.debug("exception looking for manifest in directory: " + file, ioe);
/*     */           }
/*     */         }
/* 108 */         if (man == null) {
/* 109 */           if (ZimbraLog.extensions.isDebugEnabled()) {
/* 110 */             ZimbraLog.extensions.debug("no manifest for directory: " + file);
/*     */           }
/* 112 */           return null;
/*     */         }
/*     */       }
/* 115 */     } else if (file.isFile())
/*     */     {
/*     */       try {
/* 118 */         JarFile jarFile = new JarFile(file);
/* 119 */         man = jarFile.getManifest();
/*     */       } catch (IOException ioe) {
/* 121 */         if (ZimbraLog.extensions.isDebugEnabled()) {
/* 122 */           ZimbraLog.extensions.debug("exception looking for manifest in jar file: " + file, ioe);
/*     */         }
/*     */       }
/* 125 */       if (man == null) {
/* 126 */         if (ZimbraLog.extensions.isDebugEnabled()) {
/* 127 */           ZimbraLog.extensions.debug("no manifest for jar file: " + file);
/*     */         }
/* 129 */         return null;
/*     */       }
/*     */     } else {
/* 132 */       ZimbraLog.extensions.warn("entry in extension load path is not file or directory: " + file);
/* 133 */       return null;
/*     */     }
/*     */     
/* 136 */     Attributes attrs = man.getMainAttributes();
/* 137 */     if (ZimbraLog.extensions.isDebugEnabled()) {
/* 138 */       for (Object key : attrs.keySet()) {
/* 139 */         Attributes.Name name = (Attributes.Name)key;
/* 140 */         ZimbraLog.extensions.debug("Manifest attribute=" + name + " value=" + attrs.getValue(name));
/*     */       }
/*     */     }
/* 143 */     String classname = attrs.getValue("Zimbra-Extension-Class");
/* 144 */     if (classname == null) {
/* 145 */       if (ZimbraLog.extensions.isDebugEnabled()) {
/* 146 */         ZimbraLog.extensions.debug("no extension class found in manifest of: " + file);
/*     */       }
/*     */     } else {
/* 149 */       ZimbraLog.extensions.info("extension " + classname + " found in " + file);
/*     */     }
/* 151 */     return classname;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/extension/ZimbraExtensionClassLoader.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */